//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Files.Components;
using CommunityServer.Controls;

namespace CommunityServer.Files.Controls
{
	public class Download : FileGalleryTemplatedWebControl
	{
		CSContext csContext = CSContext.Current;

		#region Child Controls

		private IText Name;
		private IText Description;

		private IButton Accept;
		private IButton Decline;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			Name = FindText( "EntryName" );
			Description = FindText( "EntryDescription" );

			Accept = FindButton( "EntryAccept" );
			Decline = FindButton( "EntryDecline" );

			InitializeChildControls();
		}

		private void InitializeChildControls() 
		{
			Accept.Click += new EventHandler(Accept_Click);
			Decline.Click += new EventHandler(Decline_Click);
		}

		#endregion

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			Entry entry = Entries.GetEntry(csContext.PostID);
			Folder f = entry.Section as Folder;
			
			if (Page.Request.UrlReferrer != null && Page.Request.Url.Host != Page.Request.UrlReferrer.Host)
			{
				if (!f.EnableExternalLinks)
					throw new CSException(CSExceptionType.AccessDenied, "External links to downloads are currently restricted.");
			}

			try
			{
				Permissions.AccessCheck(entry.Section, Permission.Read, csContext.User, entry);
			}
			catch
			{
				if (csContext.User.IsAnonymous)
					Page.Response.Redirect(SiteUrls.Instance().Login, true);
				else
					throw;
			}
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.DataBind();
		}

		public override void DataBind()
		{
			base.DataBind();

			Entry entry = Entries.GetEntry(csContext.PostID);

			if (!((Folder) entry.Section).EnableDisclaimer)
				Accept_Click(this, new System.EventArgs());

			if (Name != null)
				Name.Text = entry.Subject;

			if (Description != null)
				Description.Text = entry.RenderedBody(PostTarget.Web);

			Accept.Text = FileGalleryResourceManager.GetString("Accept");
			Decline.Text = FileGalleryResourceManager.GetString("Decline");
		}

		private void Accept_Click(object sender, EventArgs e)
		{
			int entryID = csContext.PostID;
			int userID = csContext.User.UserID;

			Entry entry = Entries.GetEntry(entryID);
			
			csContext.PostID = entryID;
			csContext.SectionID = entry.Section.SectionID;
			csContext.Section = entry.Section;

			// log the download
			UserDownloads.LogDownload(userID, entryID);

			Page.Response.Clear();

			FileGalleryAttachmentSettings fgas = FileGalleryConfiguration.GetConfig().AttachmentSettings;
			if (entry.Attachment.IsRemote)
				Page.Response.Redirect(entry.Attachment.FileName, false);
			else if (fgas.EnableDirectLinks)
				Page.Response.Redirect(Page.Response.ApplyAppPathModifier(fgas.DirectPath(entry.Attachment)));
			else
				fgas.HandleHttpRequest(this.Context, csContext.Section);

			Page.Response.End();
		}

		private void Decline_Click(Object sender, EventArgs e)
		{
			Entry entry = Entries.GetEntry(csContext.PostID);
			csContext.Context.Response.Redirect(FileGalleryUrls.Instance().ViewEntry(entry.Section.ApplicationKey, entry.PostID));
		}
	}
}
